const express = require('express')
const shopRouter = require('./shop')
const userRouter = require('./user')
const cosRouter = require('./cos')
const managerRouter = require('./manager')
const goodRouter = require('./good')
const kindRouter = require('./kind')
const ev = require('express-validation')

const router = express.Router()

router.use('/shop', shopRouter)
router.use('/user', userRouter)
router.use('/cos', cosRouter)
router.use('/manage', managerRouter)
router.use('/good', goodRouter)
router.use('/kind', kindRouter)
process.env.NODE_ENV = 'development'
if (process.env.NODE_ENV === 'development') {
  router.use((err, req, res, next) => {
    if (err instanceof ev.ValidationError) {
      res.json(err)
      return
    }
    if (!err.status) {
      res.json({ code: 1, status: 500, err, msg: '服务器错误', detail: '', data: {} })
      return
    }
    res.json({
      code: err.code,
      status: err.status,
      msg: err.msg,
      err: err.err,
      detail: err.detail,
      data: {}
    })
  })
} else {
  router.use((err, req, res, next) => {
    if (err instanceof ev.ValidationError) {
      res.json(err)
      return
    }
    if (!err.status) {
      res.json({ code: 1, status: 500, msg: '服务器错误', detail: '', data: {} })
      return
    }
    res.json({
      code: err.code,
      status: err.status,
      msg: err.msg,
      data: {}
    })
  })
}

// const router = (app) => {

//   app.use('/shop', shopRouter)
//   app.use('/user', userRouter)
// }

module.exports = router
